﻿namespace ExamV5.CardInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmCountCardList : Page
    {
        protected Button butQuery;
        protected DropDownList DDLDept;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private HttpCookie httpCookie_4 = null;
        protected Label labCardList;
        private PublicFun publicFun_0 = new PublicFun();
        private string string_0 = "0";
        protected TextBox txtEndDate;
        protected TextBox txtStartDate;

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.method_0();
        }

        protected void DDLDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.method_0();
        }

        private void method_0()
        {
            if ((this.txtStartDate.Text.Trim() != "") && !(this.txtEndDate.Text.Trim() == ""))
            {
                if (Convert.ToDateTime(this.txtStartDate.Text.Trim()) > Convert.ToDateTime(this.txtEndDate.Text.Trim()))
                {
                    base.Response.Write("<script>alert('统计时间不正确！')</script>");
                }
                else
                {
                    int num = 0;
                    this.labCardList.Text = "";
                    this.labCardList.Text = "<br><div align='center' class='style1'><strong>上岗资格证书情况报表</strong></div><br><br>";
                    this.labCardList.Text = this.labCardList.Text + "<table cellSpacing='0' cellPadding='0' width='100%' align='center'><tr><td align='left'>报告期：" + this.txtStartDate.Text.Trim() + "—" + this.txtEndDate.Text.Trim() + "</td><td align='right'>填报单位：" + this.httpCookie_4.Value + "</td></tr></table><br>";
                    this.string_0 = this.DDLDept.SelectedItem.Value;
                    this.labCardList.Text = this.labCardList.Text + "<table cellSpacing='0' cellPadding='0' width='100%' align='center' border='1' bordercolordark='#ffffff' bordercolorlight='#cccccc'  bordercolor='#cccccc'  class='tableStyle'>";
                    this.labCardList.Text = this.labCardList.Text + "<tr height=30>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center' width=30><B>序号</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center'><B>机构名称</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center' width=90><B>总人数</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center' width=90><B>持证总人数</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center' width=90><B>持证总比例</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center' width=90><B>有效持证人数</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td  align='center' width=90><B>失效持证人数</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center' width=90><B>有效证比例</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "<td align='center' width=90><B>备 注</b></td>";
                    this.labCardList.Text = this.labCardList.Text + "</tr>";
                    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                    connection.Open();
                    SqlDataReader reader = new SqlCommand("select ID,DeptName from DeptInfo where UPID=" + this.string_0 + " order by SortInfo", connection).ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        num++;
                        this.labCardList.Text = this.labCardList.Text + this.method_1(num, reader["ID"].ToString(), reader["DeptName"].ToString());
                    }
                    reader.Close();
                    connection.Close();
                    connection.Dispose();
                    this.labCardList.Text = this.labCardList.Text + "</table>";
                    this.labCardList.Text = this.labCardList.Text + "<br><table cellSpacing='0' cellPadding='0' width='100%' align='center'><tr><td align='left'>填报人：" + this.httpCookie_1.Value + "</td><td align='right'>填报日期：" + DateTime.Now.ToShortDateString() + "</td></tr></table>";
                }
            }
            else
            {
                base.Response.Write("<script>alert('统计时间不正确！')</script>");
            }
        }

        private string method_1(int int_0, string string_1, string string_2)
        {
            string values = this.publicFun_0.GetValues("select Count(*) as CountInfo from UserInfo where UserLevel='A' and Flag='A' and DeptID=" + string_1, "CountInfo");
            string str2 = this.publicFun_0.GetValues("select Count(DISTINCT a.UserCode) as CountInfo from UserInfo a,CardInfo b where a.UserCode=b.UserCode and Convert(datetime,b.CardStartDate)>=Convert(datetime,'" + this.publicFun_0.CheckString(this.txtStartDate.Text) + "') and Convert(datetime,b.CardEndDate)>=Convert(dateTime,'" + this.publicFun_0.CheckString(this.txtEndDate.Text) + "') and a.UserLevel='A' and a.Flag='A' and a.DeptID=" + string_1, "CountInfo");
            string str3 = this.publicFun_0.GetValues("select Count(DISTINCT a.UserCode) as CountInfo from UserInfo a,CardInfo b where a.UserCode=b.UserCode and Convert(datetime,b.CardStartDate)>=Convert(datetime,'" + this.publicFun_0.CheckString(this.txtStartDate.Text) + "') and Convert(datetime,b.CardEndDate)<Convert(dateTime,'" + this.publicFun_0.CheckString(this.txtEndDate.Text) + "') and a.UserLevel='A' and a.Flag='A' and a.DeptID=" + string_1, "CountInfo");
            double num = 0.0;
            int num2 = 0;
            num2 = Convert.ToInt32(str2) + Convert.ToInt32(str3);
            if (values == "")
            {
                values = "0";
            }
            if (str2 == "")
            {
                str2 = "0";
            }
            if (str3 == "")
            {
                str3 = "0";
            }
            string str4 = "";
            str4 = (((str4 + "<tr height=30>") + "<td align='center' width=30>" + int_0.ToString() + "</td>") + "<td align='center'>" + string_2 + "</td>") + "<td align='center' width=90>" + values + "</td>";
            str4 = string.Concat(new object[] { str4, "<td align='center' width=90>", num2, "</td>" });
            if (!(values == "0"))
            {
                num = Convert.ToDouble(num2) / Convert.ToDouble(values);
            }
            else
            {
                num = 0.0;
            }
            num *= 100.0;
            str4 = ((str4 + "<td align='center' width=90>" + num.ToString("0.00") + "%</td>") + "<td align='center' width=90>" + str2 + "</td>") + "<td  align='center' width=90>" + str3 + "</td>";
            if (num2 == 0)
            {
                num = 0.0;
            }
            else
            {
                num = Convert.ToDouble(str2) / Convert.ToDouble(num2);
            }
            num *= 100.0;
            return ((str4 + "<td align='center' width=90>" + num.ToString("0.00") + "%</td>") + "<td align='center' width=90>&nbsp;</td>" + "</tr>");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            this.httpCookie_4 = new HttpCookie("ExamDeptName");
            this.httpCookie_4 = base.Request.Cookies["ExamDeptName"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.httpCookie_4.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_4.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        if (this.httpCookie_2.Value == "A")
                        {
                            base.Response.Write("<script>alert('证书权限未开放！')</script>");
                        }
                        else
                        {
                            this.txtStartDate.Text = DateTime.Now.AddYears(-1).ToShortDateString();
                            this.txtEndDate.Text = DateTime.Now.ToShortDateString();
                            this.publicFun_0.LoadDeptInfo(this.DDLDept, this.httpCookie_3.Value, this.httpCookie_0.Value, this.httpCookie_2.Value);
                            if (this.httpCookie_2.Value == "C")
                            {
                                this.DDLDept.SelectedIndex = -1;
                                ListItem item = new ListItem("--所有单位--", "0");
                                this.DDLDept.Items.Add(item);
                                this.DDLDept.Items.FindByText("--所有单位--").Selected = true;
                            }
                            this.method_0();
                        }
                    }
                    else
                    {
                        base.Response.Write("<script>alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')</script>");
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

